package com.nantai.jiafang.nantaijiafang.nt5s.Activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.nantai.jiafang.nantaijiafang.R;
import com.nantai.jiafang.nantaijiafang.nt5s.IConstant;
import com.nantai.jiafang.nantaijiafang.nt5s.adapter.QualityRegulationsDataAdapter;
import com.nantai.jiafang.nantaijiafang.nt5s.bean.QualityRegulations;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * 2017/8/22 质量评议--直接评议--提取条例
 */

public class QualityRegulationsActivity extends AppCompatActivity implements View.OnClickListener, AdapterView.OnItemClickListener {


    private Context mContext;
    private ListView listView;
    private ImageView back;
    private ProgressDialog progressDialog;
    private QualityRegulationsDataAdapter qualityRegulationsDataAdapter;
    private EditText et_regulations;
    private List<QualityRegulations> mList;
    private Gson gson;
    private Button btn_screen;
    private String searchStr;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quality_regulations);
        mContext = QualityRegulationsActivity.this;
        initView();
        initData();
        getDataFromNet();
    }
    private void initView() {
        btn_screen= (Button) findViewById(R.id.btn_screen);
        back=(ImageView)findViewById(R.id.iv_back);
        progressDialog = new ProgressDialog(mContext);
        progressDialog.setMessage("正在获取数据，请稍候");
        listView=(ListView)findViewById(R.id.lv_regulations);
        et_regulations=(EditText)findViewById(R.id.et_regulations);

    }

    private void initData() {
        btn_screen.setOnClickListener(this);
        back.setOnClickListener(this);
        mList = new ArrayList<>();
        qualityRegulationsDataAdapter=new QualityRegulationsDataAdapter(mContext,mList);
        listView.setAdapter(qualityRegulationsDataAdapter);
        listView.setOnItemClickListener(this);

    }


    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }

        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }
    private void getDataFromNet() {
        RequestParams params = new RequestParams(IConstant.URL + "getQualityStandard");

        searchStr=et_regulations.getText().toString();
        params.addBodyParameter("searchStr", searchStr);

        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                System.out.println("返回结果：" + result);
                //解析result
                progressDialog.dismiss();
                gson = new Gson();
                List<QualityRegulations> list = gson.fromJson(result, new TypeToken<ArrayList<QualityRegulations>>(){}.getType());
                if(list == null){
                    return;
                }
                for(QualityRegulations rs : list){
                    System.out.println(rs.getStandard()+rs.getCardNum()+rs.getRegulationNo());
                }
                mList.addAll(list);
                qualityRegulationsDataAdapter.notifyDataSetChanged();//更新数据
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                progressDialog.dismiss();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                progressDialog.dismiss();
            }
            @Override
            public void onFinished() {
                progressDialog.dismiss();
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_screen:

                if (TextUtils.isEmpty(et_regulations.getText())){
                    Toast.makeText(getApplicationContext(), "请输入筛选条件", Toast.LENGTH_SHORT).show();
                }else{
                    getDataFromNet();
                }

                break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
}

